<template>
	<view class="page-wrap" :data-theme="theme" :class="theme" :style="globalColor.style">
		<header-custom title="我的成绩" />
		<u-tabs keyName="name" lineColor="#00C15D" :scrollable="false" :list="navList" :current="activeTab"
			@change="navChange"></u-tabs>
		<view class="addScore fboxCol Ycenter Xcenter" @tap="inquiryScore">
			<image :src="navList[activeTab].img"></image>
			<view class="tit font24 color-9899A1">{{navList[activeTab].tips}}</view>
		</view>
		<view class="space-block"></view>
		<template v-if="!editExpect.show">
		<block v-for="(item, index) in dataList.list" :key="index">
			<ScoreItem :scoreList="item" student="student" @on-delete="deleteTap" :type="navList[activeTab].key"
				@on-edit="editTap" />
		</block>
		</template>
		<score-edit v-if="editExpect.show" :scoreList="editExpect" @onCancel="editCancel" @on-save="saveTap" @on-vip="vipTips" :type="navList[activeTab].key" />
		<score-exam :show.sync="examShow" :student="student" @on-exam="examCallback" />
	</view>
</template>

<script>
	import ScoreItem from './score-item.vue';
	import scoreExam from './score-exam.vue';
	import scoreEdit from './score-edit.vue';
	import headerCustom from '@/pagesBKS/components/header-custom.vue';
	import {
		myScoreList,
		scoreAnalysisIsUpdate
	} from '@/api/bks.volunteer.js'
	export default {
		components: {
			headerCustom,
			ScoreItem,
			scoreExam,
			scoreEdit
		},
		data() {
			return {
				theme: 'bks-theme',
				globalColor: getApp().globalData.globalColor,
				navList: [{
						name: '康德联考',
						img: 'https://bks-view.obs.myhuaweicloud.com:443/css/myScore01.jpg',
						tips: '查询康德联考成绩',
						key: 1,
					},
					{
						name: '期望成绩',
						img: 'https://bks-view.obs.myhuaweicloud.com:443/css/myScore02.jpg',
						tips: '期望成绩，一周内只能修改一次',
						key: 2,
					},
					{
						name: '高考成绩',
						img: 'https://bks-view.obs.myhuaweicloud.com:443/css/myScore02.jpg',
						tips: '高考成绩，一旦提交不可修改',
						key: 3,
					}
				],
				activeTab: 0,
				dataList: {
					list: [],
					page: 1,
					size: 20,
					count: 0,
					loading: 'none'
				},
				examShow: false,
				subjectShow: false,
				student: {},
				editUniversity: {
					show: false,
					scoreInfos: []
				},
				editExpect: {
					show: false,
					scoreInfos: []
				},
				showVipDialog: false,
				filterList: [{
						id: 'chinese',
						subId: '',
						max: 150,
						subjectName: '语文',
						subjectScore: '',
						fax: false,
						faxScore: ''
					},
					{
						id: 'mathematics',
						subId: '',
						max: 150,
						subjectName: '数学',
						subjectScore: '',
						fax: false,
						faxScore: ''
					},
					{
						id: 'english',
						subId: '',
						max: 150,
						subjectName: '外语',
						subjectScore: '',
						fax: false,
						faxScore: ''
					},
					{
						id: 'physical',
						subId: '',
						max: 100,
						subjectName: '物理',
						subjectScore: '',
						fax: false,
						faxScore: ''
					},
					{
						id: 'history',
						subId: '',
						max: 100,
						subjectName: '历史',
						subjectScore: '',
						fax: false,
						faxScore: ''
					},
					{
						id: 'biological',
						subId: 'newBiological',
						max: 100,
						subjectName: '生物学',
						subjectScore: '',
						fax: false,
						faxScore: ''
					},
					{
						id: 'chemical',
						subId: 'newChemical',
						max: 100,
						subjectName: '化学',
						subjectScore: '',
						fax: false,
						faxScore: ''
					},
					{
						id: 'political',
						subId: 'newPolitical',
						max: 100,
						subjectName: '思政',
						subjectScore: '',
						fax: false,
						faxScore: ''
					},
					{
						id: 'geographic',
						subId: 'newGeographic',
						max: 100,
						subjectName: '地理',
						subjectScore: '',
						fax: false,
						faxScore: ''
					},
				]
			}
		},
		onLoad(option) {
			this.student = uni.getStorageSync('bw_bks_student') || {}
		},
		onShow() {
			this._request()
		},
		methods: {
			navChange(e) {
				this.activeTab = e.index
				this.dataList = {
					list: [],
					page: 1,
					size: 20,
					count: 0,
					loading: 'loading'
				}
				this._request()
			},
			examCallback() {
				this.examShow = false
			},
			// 查询成绩
			inquiryScore() {
				const bw_bks_params = uni.getStorageSync('bw_bks_params') || {}
				if (bw_bks_params.access_token) {
					if (this.student.studentName) {
						this.examShow = true
					} else {
						this.$pageTo('/bks/mine/certification/index')
					}
				}
			},
			// 请求成绩列表
			_request() {
				myScoreList({
					type: this.navList[this.activeTab].key
				}).then(res => {
					const list = []
					res.data.filter((item, index) => {
						list.push(this.dataReset(item, index))
					})
					this.dataList.list = list
				})
			},
			// 重置成绩
			dataReset(data, index) {
				const filterList = [{
						id: 'chinese',
						subId: '',
						max: 150,
						subjectName: '语文',
						subjectScore: '',
						fax: false,
						faxScore: ''
					},
					{
						id: 'mathematics',
						subId: '',
						max: 150,
						subjectName: '数学',
						subjectScore: '',
						fax: false,
						faxScore: ''
					},
					{
						id: 'english',
						subId: '',
						max: 150,
						subjectName: '英语',
						subjectScore: '',
						fax: false,
						faxScore: ''
					},
					{
						id: 'physical',
						subId: '',
						max: 100,
						subjectName: '物理',
						subjectScore: '',
						fax: false,
						faxScore: ''
					},
					{
						id: 'history',
						subId: '',
						max: 100,
						subjectName: '历史',
						subjectScore: '',
						fax: false,
						faxScore: ''
					},
					{
						id: 'biological',
						subId: 'newBiological',
						max: 100,
						subjectName: '生物学',
						subjectScore: '',
						fax: false,
						faxScore: ''
					},
					{
						id: 'chemical',
						subId: 'newChemical',
						max: 100,
						subjectName: '化学',
						subjectScore: '',
						fax: false,
						faxScore: ''
					},
					{
						id: 'political',
						subId: 'newPolitical',
						max: 100,
						subjectName: '思政',
						subjectScore: '',
						fax: false,
						faxScore: ''
					},
					{
						id: 'geographic',
						subId: 'newGeographic',
						max: 100,
						subjectName: '地理',
						subjectScore: '',
						fax: false,
						faxScore: ''
					},
				]
				const dataList = {
					...data,
					scoreInfos: []
				}
				for (let i in data) {
					if (data[i] != 'null' && data[i] != null) {
						filterList.filter(item => {
							if (item.id == i) {
								item.subjectScore = data[i]
								dataList.scoreInfos.push(item)
							} else if (item.subId == i) {
								item.faxScore = data[i]
								if (this.activeTab == 3) {
									item.subjectScore = data[i]
									item.fax = true
									dataList.scoreInfos.push(item)
								}
							}
						})
					}
				}
				return dataList
			},
			onVipClick() {
				this.showVipDialog = false
			},
			// vip提示
			vipTips() {
				this.showVipDialog = true
			},
			// 保存
			saveTap() {
				if (this.activeTab == 2) {
					this.editExpect = { show: false, scoreInfos: [] }
					this.subjectShow = false
				} else if (this.activeTab == 3) {
					this.editUniversity = { show: false, scoreInfos: [] }
					this.subjectShow = false
				}
				this._request()
			},
			// 选择科目
			subjectDefine(e) {
				if (e.type == 2) {
					this.editExpect = {
						show: true,
						scoreInfos: e.subjectList
					}
					this.subjectShow = false
				} else {
					this.editUniversity = {
						show: true,
						scoreInfos: e.subjectList
					}
					this.subjectShow = false
				}
			},
			// 选择考试
			examDefine(e) {
				this.examShow = false
				this._request()
			},
			// 取消编辑
			editCancel(e) {
				console.log(e)
				if (e.type == 2) {
					this.editExpect = {
						show: false,
						scoreInfos: []
					}
				} else {
					this.editUniversity = {
						show: false,
						scoreInfos: []
					}
				}
			},
			// 编辑
			editTap(e) {
				scoreAnalysisIsUpdate({
					type: 2
				}).then(res => {
					if (res.data) {
						if (this.activeTab == 0) {
							this.activeTab = 1
						}
						this.subjectShow = false
						this.editExpect = {
							...e,
							show: true
						}
					} else {
						uni.showToast({
							title: '期望成绩，一周内只能修改一次',
							icon: 'none'
						})
					}
				}).catch(err=>{
					uni.showToast({
						title: '期望成绩，一周内只能修改一次',
						icon: 'none'
					})
				})
			},
			// 期望成绩
			expectTap() {
				if (!this.editExpect.show) {
					if (this.dataList.list.length) {
						uni.showToast({
							title: '期望成绩，只能添加一次',
							icon: 'none'
						})
					} else {
						if (this.getIsLogin()) {
							this.subjectShow = true
						}
					}
				}
			},
			getIsLogin() {
				const bw_bks_params = uni.getStorageSync('bw_bks_params') || {}
				if (bw_bks_params.access_token) {
					return true
				} else {
					this.$pageTo('/bks/login/index/index')
				}
			},
			// 高考成绩
			university() {
				// 是否可以修改期望成绩
				scoreAnalysisIsUpdate({
					type: 3
				}).then(res => {
					if (!this.editUniversity.show && res.data) {
						if (this.dataList.list.length) {
							uni.showToast({
								title: '高考成绩，一旦提交不可修改',
								icon: 'none'
							})
						} else {
							if (this.getIsLogin()) {
								this.subjectShow = true
							}
						}
					} else {
						uni.showToast({
							title: '暂不能添加高考成绩',
							icon: 'none'
						})
					}
				}).catch(err=>{
					uni.showToast({
						title: '暂不能添加高考成绩',
						icon: 'none'
					})
				})
			},
		}
	}
</script>

<style lang="scss" scoped>
	.module {
		padding: 30rpx 30rpx 40rpx;
	}

	.moreTitle {
		height: 44rpx;
	}

	.moreTitle .bksCIcon {
		margin-left: 10rpx;
	}

	.banner image {
		width: 750rpx;
		height: 140rpx;
	}

	.newsMsg {
		position: relative;
		padding: 0 80rpx 0 30rpx;
		width: 750rpx;
		line-height: 80rpx;
		height: 80rpx;
		text-overflow: ellipsis;
		white-space: nowrap;
		overflow: hidden;
	}

	.newsMsg .bksCIcon {
		margin-right: 10rpx;
	}

	.mt20 {
		margin-top: 20rpx;
	}

	.mt30 {
		margin-top: 30rpx;
	}

	.date {
		padding: 0 30rpx;
		height: 68rpx;
		background-color: #F5F5FA;
	}

	.subjectList {}

	.subjectList .item {
		padding: 0 30rpx;
		height: 108rpx;
		border-bottom: 1px solid #F0F0F5;
	}

	.red {
		color: #E95241
	}

	.addScore {
		padding: 30rpx 0 40rpx;
	}

	.addScore image {
		width: 201rpx;
		height: 184rpx;
	}
</style>